<template>
  <div>
    <div v-if="activityView.hasActivityPower === true || activityView.hasAuth === true">
      <el-radio-group v-if="activityView.hasActivityPower === false" v-model="isCollapse" @change="changeScope()" style="margin-bottom: 20px;">
        <el-radio-button :label="false">我的</el-radio-button>
        <el-radio-button :label="true">全部</el-radio-button>
      </el-radio-group>

      <div v-if="activityView.hasActivityPower === false && isCollapse === false">
        <el-input type="textarea" :rows="9" placeholder="请输入内容" v-model="content" maxlength="1000" show-word-limit>
        </el-input>
        <div style="text-align: center; margin: 10px 0;">
          <el-button size="medium" type="warning" @click="addMoment">发布打卡</el-button>
        </div>
      </div>

      <el-card style="display: block;margin: 20px auto 0 auto;" v-show="momentList.length > 0">
        <div v-for="(item, index) in momentList" :key="item.momentId">
          <div style="overflow: hidden; height: 60px;">
            <div>
              <div style="float: left;">
                <el-avatar :size="50" :src="mediaHost + item.userView.avatarUrl"></el-avatar>
              </div>
              <div style="float: left; margin-top: 18px; margin-left: 10px;">
                {{ item.userView.nickName }}
              </div>
              <div style="float: left; margin-top: 18px; margin-left: 10px;">
                {{ item.createTimeStr }}
              </div>
              <div v-show="isCollapse === false && activityView.hasActivityPower === false" style="float: left; margin-top: 15px; margin-left: 20px;">
                <el-button size="mini" type="text" icon="el-icon-delete"  @click="canDelMoment(item.momentId)"></el-button>
              </div>
            </div>
          </div>
          <div style="font-size: small">打卡内容：{{ item.content }}</div>
          <el-divider></el-divider>
        </div>
      </el-card>

      <!--请求标识-->
      <div v-if="loadingFlag=== true" style="text-align: center;font-size: larger;margin-top: 10px;">
        <i  class="el-icon-loading"></i>
      </div>
      <div v-else-if="indexPage.hasMore === true" style="text-align: center;font-size: small;color: #99a9bf;margin-top: 10px;" @click="loadingMore()">
        <span>--点击加载更多--</span>
      </div>
      <div v-else-if="momentList.length > 0" style="text-align: center;font-size: small;color: #99a9bf;margin-top: 10px;">
        <span>--没有更多数据了--</span>
      </div>
      <div v-else style="text-align: center;font-size: small;color: #99a9bf;margin-top: 10px;">
        <span>--暂无打卡数据--</span>
      </div>
    </div>
    <div v-else>
      <div style="text-align: center;font-size: small;color: #99a9bf;margin-top: 10px;">
        <span>--请先报名--</span>
      </div>
    </div>

    <!--删除提示框-->
    <el-dialog title="提示" :visible.sync="deleteDialogVisible" width="20%">
      <span>确认删除此打卡内容吗？</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="delMoment()">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Moment",
  props: ['activityId'],
  data() {
    return {
      mediaHost: this.$global.mediaHost,
      indexPage: this.$global.indexPage,
      activityView: {
        creator: '',
        activityName: '',
        activityType: '',
        activityLogo: '',
        activityDesc: '',
        teacherAvatarUrl: this.$global.avatarDefault,
        createTimeStr: '',
        authNum: '',
        hasAbility: false,
        hasAuth: false,
        hasActivityPower: false
      },
      paramsData: {
        activityId: ''
      },
      content:'',
      isCollapse: false,
      momentList:[],
      loadingFlag: false,
      hasOnlyMyMoment: true,
      delMomentId : '',
      deleteDialogVisible : false
    }
  },
  mounted() {
    addEventListener('scroll', this.handleScroll)//监听函数
  },
  methods: {
    handleScroll() {
      //变量scrollTop是滚动条滚动时，距离顶部的距离
      let scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
      //变量windowHeight是可视区的高度
      let windowHeight = document.documentElement.clientHeight || document.body.clientHeight;
      //变量scrollHeight是滚动条的总高度
      let scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight;
      //滚动条到底部的条件
      if (scrollTop + windowHeight === scrollHeight) {
        //你想做的事情
        if (this.indexPage.hasMore === true && !this.loadingFlag) {
          this.indexPage.page += 1;
          this.getMomentList();
        }
      }
    },
    loadingMore:function () {
      this.indexPage.page += 1;
      this.getMomentList();
    },
    /**
     * 个人/全部
     */
    changeScope:function () {
      this.indexPage = this.$global.indexPage
      this.momentList = []
      this.loadingFlag = false
      this.hasOnlyMyMoment = this.isCollapse === false;
      this.getMomentList()
    },
    canDelMoment:function (delMomentId) {
      this.deleteDialogVisible = true
      this.delMomentId = delMomentId
    },
    /**
     *
     * 班级详情
     * */
    detail: function () {
      this.paramsData.activityId = this.activityId
      this.$ajax({
        method: 'post',
        headers: this.$global.headers,
        url: this.$global.host + '/v1/activity/detail',
        data: JSON.stringify({
          "data": this.paramsData,
          "user": this.$global.getUser(),
          "teamId": this.$global.getTeamId()
        }),
      }).then(response => {
        const responseData = response.data.data;
        const responseState = response.data.state
        if (2000 === responseState.code) {
          this.activityView = responseData.activityView
          //如果是老师，就展示全部人的打卡
          this.hasOnlyMyMoment = this.activityView.hasActivityPower === false;
          this.getMomentList()
        } else {
          this.$message.error({
            message: responseState.msg,
            center: true,
            duration: 1000
          });
        }
      }).catch(function (err) {
        console.log(err)
      })
    },
    /**
     *
     * 添加打卡
     * */
    addMoment: function () {
      if (this.content === '') {
        this.$message.warning({
          message: '请输入打卡内容',
          center: true,
          duration: 1000
        });
        return false
      }
      this.paramsData.activityId = this.activityId
      this.paramsData.content = this.content
      this.$ajax({
        method: 'post',
        headers: this.$global.headers,
        url: this.$global.host + '/v1/moment/addMoment',
        data: JSON.stringify({
          "data": this.paramsData,
          "user": this.$global.getUser(),
          "teamId": this.$global.getTeamId()
        }),
      }).then(response => {
        const responseData = response.data.data;
        const responseState = response.data.state
        if (2000 === responseState.code) {
          this.$message.success({
            message: "打卡成功",
            center: true,
            duration: 1000
          });
          this.content = ''
          this.indexPage = this.$global.indexPage
          this.momentList = []
          this.loadingFlag = false
          this.getMomentList()
        } else {
          this.$message.error({
            message: responseState.msg,
            center: true,
            duration: 1000
          });
        }
      }).catch(function (err) {
        console.log(err)
      })
    },
    /**
     *获取查看课件记录
     *
     */
    getMomentList: function () {
      this.loadingFlag = true
      this.paramsData.activityId = this.activityId
      this.paramsData.hasOnlyMyMoment = this.hasOnlyMyMoment
      this.paramsData.page = this.indexPage
      this.$ajax({
        method: 'post',
        headers: this.$global.headers,
        url: this.$global.host + '/v1/moment/getMomentList',
        data: JSON.stringify({
          "data": this.paramsData,
          "user": this.$global.getUser(),
          "teamId": this.$global.getTeamId()
        }),
      }).then(response => {
        const responseData = response.data.data;
        const responseState = response.data.state
        if (2000 === responseState.code) {
          this.loadingFlag = false
          this.momentList = this.momentList.concat(responseData.pageData.data)
          this.indexPage = responseData.pageData.page
        } else {
          this.$message.error({
            message: responseState.msg,
            center: true,
            duration: 1000
          });
        }
      }).catch(function (err) {
        console.log(err)
      })
    },
    /**
     *
     * 删除打卡
     * */
    delMoment: function () {
      this.deleteDialogVisible = false
      this.paramsData.activityId = this.activityId
      this.paramsData.momentId = this.delMomentId
      this.$ajax({
        method: 'post',
        headers: this.$global.headers,
        url: this.$global.host + '/v1/moment/delMoment',
        data: JSON.stringify({
          "data": this.paramsData,
          "user": this.$global.getUser(),
          "teamId": this.$global.getTeamId()
        }),
      }).then(response => {
        const responseData = response.data.data;
        const responseState = response.data.state
        if (2000 === responseState.code) {
          this.$message.success({
            message: "操作成功",
            center: true,
            duration: 1000
          });
          this.indexPage = this.$global.indexPage
          this.momentList = []
          this.loadingFlag = false
          this.getMomentList()
        } else {
          this.$message.error({
            message: responseState.msg,
            center: true,
            duration: 1000
          });
        }
      }).catch(function (err) {
        console.log(err)
      })
    },
  },
  created() {
    this.detail()
  }
}
</script>

<style scoped>

</style>
